package com.example.testapplication;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

public class GuidePage extends Activity {
    private static final String TAG = "GuidePage";
    private ViewPager vpGuiding;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_guide_page);

        SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences",0);
        boolean isFirstTime = sharedPreferences.getBoolean("isFirstTime",true);
        if (isFirstTime){
            setContentView(R.layout.activity_guide_page);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("isFirstTime",false);
            editor.apply();
            initView();
        }
        else {
            Intent intent = new Intent(this,MainActivity.class);
            startActivity(intent);
            finish();
        }
    }

    private void initView() {
        final View view1 = View.inflate(this, R.layout.activity_guide_1, null);
        view1.findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToMainPage();
            }
        });
        view1.findViewById(R.id.btnNext).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeView(1);
            }
        });
        final View view2 = View.inflate(this, R.layout.activity_guide_2, null);
        view2.findViewById(R.id.btnNext).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                changeView(2);
            }
        });
        view2.findViewById(R.id.btnSkip).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToMainPage();
            }
        });
        final View view3 = View.inflate(this, R.layout.activity_guide_3, null);
        view3.findViewById(R.id.btnEnter).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                jumpToMainPage();
            }
        });

        List<View> list = new ArrayList<>();
        list.add(view1);
        list.add(view2);
        list.add(view3);
        vpGuiding = findViewById(R.id.viewPager);
        //适配器赋值
        ViewPagerAdapter myPagerAdapter = new ViewPagerAdapter(list);
        //绑定数据适配器
        vpGuiding.setAdapter(myPagerAdapter);


        vpGuiding.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                Log.d(TAG, "onPageScrolled() called with: position = [" + position + "], positionOffset = [" + positionOffset + "], positionOffsetPixels = [" + positionOffsetPixels + "]");
            }

            @Override
            public void onPageSelected(final int position) {
                Log.d(TAG, "onPageSelected() called with: position = [" + position + "]");
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                Log.d(TAG, "onPageScrollStateChanged() called with: state = [" + state + "]");
            }
        });
    }

    private void jumpToMainPage() {
        Intent intent = new Intent(GuidePage.this, MainActivity.class);
        startActivity(intent);
        finish();
    }

    private void changeView(int change) {
        vpGuiding.setCurrentItem(change);
    }

    private void firstTime(){
        SharedPreferences sharedPreferences = getSharedPreferences("sharedPreferences",0);
        boolean isFirstTime = sharedPreferences.getBoolean("isFirstTime",true);
        if (isFirstTime){
            setContentView(R.layout.activity_guide_page);
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putBoolean("isFirstTime",false);
            editor.apply();
            initView();
        }
        else {
            setContentView(R.layout.activity_main);
        }
    }
}



